package com.hcbd.tdwt.ui.xingyu.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.hcbd.tdwt.R;


public class ForegroundService extends Service {
    private final static int SERVICE_ID = 1;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.e("ForegroundService:","onCreate");
        super.onCreate();

        if (Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN_MR2){
            //4.3以下
            startForeground(SERVICE_ID,new Notification());
        }else if (Build.VERSION.SDK_INT<Build.VERSION_CODES.O){
            //7.0以下
            startForeground(SERVICE_ID,new Notification());
            //删除通知栏
            startService(new Intent(this,InnerService.class));
        }else {
                /*//8.0以上
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                //NotificationManager.IMPORTANCE_MIN 通知栏消息的重要级别  最低，不让弹出
                //IMPORTANCE_MIN 前台时，在阴影区能看到，后台时 阴影区不消失，增加显示 IMPORTANCE_NONE时 一样的提示
                //IMPORTANCE_NONE app在前台没有通知显示，后台时有
                NotificationChannel channel = new NotificationChannel("channel", "天应北斗", NotificationManager.IMPORTANCE_NONE);
                if (notificationManager!=null){
                    notificationManager.createNotificationChannel(channel);
                    Notification notification = new Notification.Builder(this, "channel").build();
                    startForeground(SERVICE_ID,notification);
                }*/

            String CHANNEL_ID = "NFCService";
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel Channel = new NotificationChannel(CHANNEL_ID, "北斗模块", NotificationManager.IMPORTANCE_HIGH);
            Channel.enableLights(true);//设置提示灯
            Channel.setLightColor(Color.RED);//设置提示灯颜色
            Channel.setShowBadge(true);//显示logo
            Channel.setDescription("bottombar notification");//设置描述
            Channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); //设置锁屏可见 VISIBILITY_PUBLIC=可见
            if (manager != null) {
                manager.createNotificationChannel(Channel);
            }
            Notification notification = new Notification.Builder(this)
                    .setChannelId(CHANNEL_ID)
                    .setAutoCancel(false)
                    .setContentTitle("北斗服务")//标题
                    .setContentText("运行中")//内容
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.logo)//小图标一定需要设置,否则会报错(如果不设置它启动服务前台化不会报错,但是你会发现这个通知不会启动),如果是普通通知,不设置必然报错
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))
                    .build();
            startForeground(1, notification);
        }
    }


    private static class InnerService extends Service{
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            // Log.d(KeepAliveApp.TAG, "onCreate: ");
            startForeground(SERVICE_ID,new Notification());
            stopSelf();
        }
    }


}
